package com.mgface.eth.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web3j.crypto.WalletUtils;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * @author wanyuxiang
 * @version 1.0
 * @desc 生成账户的类
 * @project web3jEthereum
 * @create 2018-06-25 19:09
 **/
public class AutoGenAccount {

	private static final Logger log = LoggerFactory.getLogger(AutoGenAccount.class);
	/**
	 *
	 * @param password 输入密码
	 */
	public static String gen(String password) throws Exception {
		Path path = Files.createTempDirectory(null);
		System.out.println(path);
		String fileName  = WalletUtils.generateNewWalletFile(password,path.toFile(),Boolean.TRUE);
		log.info("生成的钱包私钥文件名:{}",fileName);
		String keyFile = path.toString()+ File.separator+fileName;
		log.info(keyFile);
		Path p = new File(keyFile).toPath();
		//将文件所有内容读入byte数组中
		byte[] bytes = Files.readAllBytes(p); //传入Path对象
		 //之后可以根据字符集构建字符串
		String content = new String(bytes, "UTF-8");
		log.info("密钥文件:{}",content);
		return content;
	}

	public static void main(String[] args) {
		try {
			String keystore = gen("123456");
			System.out.println(keystore);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
